#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 插入3列
    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(1);
    ui->tableWidget->insertColumn(2);
    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("编号")); // 水平方向上的表头标题
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("姓名"));
    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("爱好"));

    ui->tableWidget->insertRow(0);
    ui->tableWidget->insertRow(1);
    ui->tableWidget->insertRow(2);
    ui->tableWidget->setVerticalHeaderItem(0,new QTableWidgetItem("1")); // 垂直方向上的表头标题
    ui->tableWidget->setVerticalHeaderItem(1,new QTableWidgetItem("2"));
    ui->tableWidget->setVerticalHeaderItem(2,new QTableWidgetItem("3"));


}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_insertRow_clicked()
{
    // 插入一行
    int row = ui->tableWidget->rowCount();
    QString text = ui->lineEdit->text();
    if(text == "")
        return;
    QTableWidgetItem* item = new QTableWidgetItem(text);
    ui->tableWidget->insertRow(row);
    ui->tableWidget->setVerticalHeaderItem(row,item);
}

void Widget::on_pushButton_removeRow_clicked()
{
    // 删除行
    int row = ui->tableWidget->currentRow();
    if(row == -1)
        return;
    ui->tableWidget->removeRow(row);
}

void Widget::on_pushButton_insertCol_clicked()
{
    int col = ui->tableWidget->columnCount();
    QString text = ui->lineEdit->text();
    if(text == "")
        return;
    QTableWidgetItem* item = new QTableWidgetItem(text);
    ui->tableWidget->insertColumn(col);
    ui->tableWidget->setHorizontalHeaderItem(col,item);
}

void Widget::on_pushButton_RemoveCol_clicked()
{
    int col = ui->tableWidget->currentColumn();
    if(col == -1)
        return;
    ui->tableWidget->removeColumn(col);
}
